package com.sword.springboot.springboot_rabbitmq.tut2;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * com.sword.springboot.springboot_rabbitmq.tut2.Tut2Sender
 * Created by SwordLiu on 2017-11-15.
 */
public class Tut2Sender {

    @Autowired
    private RabbitTemplate template;
    @Autowired
    private Queue queue;

    private int dots = 0;
    private int count = 0;

    @Scheduled(fixedDelay = 1000, initialDelay = 500)
    public void send() {
        StringBuilder builder = new StringBuilder("hello");
        if (dots++ == 5) {
            dots = 1;
        }

        for(int i = 0; i < dots; i++) {
            builder.append(".");
        }

        builder.append(Integer.toString(++count));
        String message = builder.toString();
        template.convertAndSend(queue.getName(), message);
        System.out.println(" [x] Sent '" + message + "'");
    }

}
